package com.cdtu.store.item.service.impl;

import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.store.common.utils.PageUtils;
import com.cdtu.store.common.utils.Query;
import com.cdtu.store.item.dao.StockDao;
import com.cdtu.store.common.entity.StockEntity;
import com.cdtu.store.item.service.SkuService;
import com.cdtu.store.item.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("stockService")
public class StockServiceImpl extends ServiceImpl<StockDao, StockEntity> implements StockService {
    @Autowired
    private SkuService skuService;

    @Override
    public PageUtils list(Map<String, Object> params) {

        QueryWrapper<StockEntity> wrapper = new QueryWrapper<StockEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and((w) -> {
                w.eq("sku_id",key);
            });
        }
        IPage<StockEntity> page = this.page(
                new Query<StockEntity>().getPage(params),
                wrapper
        );
        List<StockEntity> list = page.getRecords();
        list.forEach((stock -> {
            stock.setSname(skuService.getById(stock.getSkuId()).getTitle());
        }));
        return new PageUtils(page);
    }
}